/*===================================================================
enrollments.do
	*Started:	
	*Updated:	2021-09-15
	*Author(s):	David Phillips
	*Purpose:	Creates person-level outcomes for financial assistance
				payments from homelessness prevention programs in HMIS
===================================================================*/

	

*connecting payment data to start dates from intake
	clear
	import excel using "$datadir/HP Services Data.xlsx", firstrow clear

	rename ClientsUniqueIdentifier HMISID
	merge m:1 HMISID using "$datadir/assmt.dta", keepusing(HMISID startdate)
	*1's are largely from outside the program timeframe
	drop if _merge == 1
	drop _merge


*computing outcomes

	gen rent = 0
	replace rent = 1 if regexm(ServicesServiceItemName,"Rent")
	
	gen sd = 0
	replace sd  = 1 if regexm(ServicesServiceItemName,"Security")

	gen util = 0
	replace util = 1 if regexm(ServicesServiceItemName,"Util")
	
	gen other_hous = 0
	replace other_hous = 1 if regexm(ServicesServiceItemName,"hous")
	replace other_hous = 1 if regexm(ServicesServiceItemName,"Hous")
	replace other_hous = 1 if regexm(ServicesServiceItemName,"Mov")
	replace other_hous = 1 if regexm(ServicesServiceItemName,"Motel")
	replace other_hous = 0 if ServicesServiceItemName == "Household Expenses"
	
	gen trans = 0
	replace trans = 1 if regexm(ServicesServiceItemName, "Transportation")

	
	gen other = 1
	foreach var of varlist rent sd {
		replace other = 0 if `var' == 1
	}
	*replace other = 1 if sd == 0 & rent == 0 & util == 0 & ServicesServiceItemName ~= ""

	*within one month of a security deposit payment
	gen nearsd = 0
	replace nearsd = 1 if sd == 1
	sort HMISID ServiceExpensesDateDate
	forvalues x = 1(1)30 {
		replace nearsd = 1	if 	sd[_n-`x'] == 1 ///
								& HMISID == HMISID[_n-`x'] ///
								& abs(ServiceExpensesDateDate - ServiceExpensesDateDate[_n-`x']) <= 30

		replace nearsd = 1	if 	sd[_n+`x'] == 1 ///
								& HMISID == HMISID[_n+`x'] ///
								& abs(ServiceExpensesDateDate - ServiceExpensesDateDate[_n+`x']) <= 30
	}

	gen tot = 1
		
	foreach y in tot rent sd util other nearsd other_hous trans {
	forvalues x = 1(1)24 {
		gen paymt_`y'_`x' = 0		
		replace paymt_`y'_`x' = ServiceExpensesAmount if `y' == 1 & ServiceExpensesDateDate < startdate + 30*`x' & ServiceExpensesDateDate >= startdate
		
		gen paymtany_`y'_`x' = 0		
		replace paymtany_`y'_`x' = 1 if `y' == 1 & ServiceExpensesDateDate < startdate + 30*`x' & ServiceExpensesDateDate >= startdate
				
	}
	}

	forvalues x = 1(1)24 {	
		gen paymtnum_tot_`x' = paymtany_tot_`x'
	}

*collapsing to totals by person
	gen month = floor((ServiceExpensesDateDate - startdate)/30)
	collapse (sum) paymt_* (max) paymtnum_* paymtany_* startdate, by(HMISID month)
	collapse (sum) paymt_* paymtnum_* (max) paymtany_* startdate, by(HMISID)

	
*missing values beyond time horizon
	foreach y in tot rent sd util other nearsd other_hous trans {
	forvalues x = 1(1)24 {
		replace paymt_`y'_`x' = . if startdate + 30*`x' > date("9/14/2021","MDY")	
		replace paymtany_`y'_`x' = . if startdate + 30*`x' > date("9/14/2021","MDY")
		replace paymtnum_tot_`x' = . if startdate + 30*`x' > date("9/14/2021","MDY")
	}
	}

	
*labeling
	forvalues x = 1(1)24 {
		label var paymtany_tot_`x' "Any HP Payment Made (`x' Mos)"
		label var paymt_tot_`x' "Total Payments (`x' Mos)"
		label var paymtany_rent_`x' "Rent HP Payment Made (`x' Mos)"
		label var paymt_rent_`x' "Rent Payments (`x' Mos)"		
	}


*saving
	save "$datadir/paymt.dta", replace
	

